package com.fragrance.controller;

import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.alibaba.fastjson.JSON;
import com.fragrance.service.WeChatUserService;
import com.fragrance.service.impl.WeChatUserServiceImpl;
import com.fragrance.util.Constants;
import com.fragrance.util.Encrypt;
import com.jfinal.core.Controller;
import com.jfinal.plugin.redis.Redis;

public class WeChatInnerUserController extends Controller {

	private Logger logger = LoggerFactory.getLogger(WeChatInnerUserController.class);

	private WeChatUserService weChatUserService = new WeChatUserServiceImpl();

	/**
	 * 判断用户是否已经绑定openid
	 */
	public void isBindWeChatUser() {
		String openid = getPara("openid");
		Map<String, String> result = new HashMap<String, String>();
		if (StringUtils.isEmpty(openid)) {
			logger.info("openid不能为空");
			result.put("code", Constants.PARAM_VAILD_CODE);
			result.put("msg", Constants.PARAM_VAILD_MSG);
		} else {
			try {
				boolean flag = weChatUserService.existOpenid(openid);
				logger.info("用戶是否存在: " + flag);
				result.put("code", Constants.SUCCESS_CODE);
				result.put("msg", Constants.SUCCESS_MSG);
				result.put("data", String.valueOf(flag));
			} catch (Exception e) {
				logger.error("判断用户是否已经绑定异常", e);
				result.put("code", Constants.ERROR_CODE);
				result.put("msg", Constants.ERROR_MSG);
			}
		}
		renderJson(result);
	}

	/**
	 * 保存微信用戶信息
	 */
	public void saveWeChatUserInfo() {
		String openid = getPara("openid");
		String nickName = getPara("nickName");
		String province = getPara("province");
		String city = getPara("city");
		String country = getPara("country");

		Map<String, Object> result = new HashMap<String, Object>();
		if (StringUtils.isBlank(openid)) {
			logger.info("openid不能为空");
			result.put("code", Constants.PARAM_VAILD_CODE);
			result.put("msg", Constants.PARAM_VAILD_MSG);
			return;
		}
		Map<String, Object> userInfo = new HashMap<String, Object>();
		try {
			int gender = getParaToInt("gender");
			userInfo.put("openid", openid);
			// 小程序过来的把openid作为用户的accountNo
			userInfo.put("accountNo", openid);
			userInfo.put("nickName", nickName);
			userInfo.put("province", province);
			userInfo.put("city", city);
			userInfo.put("sex", String.valueOf(gender));
			userInfo.put("country", country);
			weChatUserService.saveWeChatUserInfo(userInfo);
			// 把用户信息存到redis 同时返回token给前端
			String token = Encrypt.MD5("wx_" + Encrypt.MD5(openid, "utf-8"));
			String userInfoStr = Redis.use("users").get(token);
			if (StringUtils.isNotBlank(userInfoStr)) {
				// 说明微信用户已经授权过
				Map<String, String> userInfoMap = JSON.parseObject(userInfoStr, Map.class);
				String sessionKey = userInfoMap.get("sessionKey");
				userInfo.put("sessionKey", sessionKey);
				userInfo.put("token", token);
				userInfo.put("hashUserInfo", true);
				Redis.use("users").setex(token, 24 * 60 * 60, JSON.toJSONString(userInfo));
				// 该值是密钥不能返回前端
				userInfo.remove("sessionKey");
				// 添加返回微信用户信息
				result.put("code", Constants.SUCCESS_CODE);
				result.put("msg", Constants.SUCCESS_MSG);
				result.put("data", userInfo);
				renderJson(result);
				return;
			} else {
				logger.info("没有获取到微信的授权信息");
				result.put("code", Constants.ERROR_CODE);
				result.put("msg", Constants.ERROR_MSG);
			}
		} catch (Exception e) {
			logger.error("保存微信用戶信息异常", e);
			result.put("code", Constants.ERROR_CODE);
			result.put("msg", Constants.ERROR_MSG);
		}
		renderJson(result);
	}

}